package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.cld.domain.CldInvoice;
import com.cleaningcloud.cld.mapper.CldInvoiceMapper;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldInvoiceDeductionMapper;
import com.cleaningcloud.cld.domain.CldInvoiceDeduction;
import com.cleaningcloud.cld.service.ICldInvoiceDeductionService;

/**
 * 发票扣款Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2025-04-26
 */
@Service
public class CldInvoiceDeductionServiceImpl implements ICldInvoiceDeductionService 
{
    @Autowired
    private CldInvoiceDeductionMapper cldInvoiceDeductionMapper;
    @Autowired
    private CldInvoiceMapper cldInvoiceMapper;

    /**
     * 查询发票扣款
     * 
     * @param invoiceDeductionId 发票扣款主键
     * @return 发票扣款
     */
    @Override
    public CldInvoiceDeduction selectCldInvoiceDeductionByInvoiceDeductionId(Long invoiceDeductionId)
    {
        return cldInvoiceDeductionMapper.selectCldInvoiceDeductionByInvoiceDeductionId(invoiceDeductionId);
    }

    /**
     * 查询发票扣款列表
     * 
     * @param cldInvoiceDeduction 发票扣款
     * @return 发票扣款
     */
    @Override
    public List<CldInvoiceDeduction> selectCldInvoiceDeductionList(CldInvoiceDeduction cldInvoiceDeduction)
    {
        return cldInvoiceDeductionMapper.selectCldInvoiceDeductionList(cldInvoiceDeduction);
    }

    /**
     * 新增发票扣款
     * 
     * @param cldInvoiceDeduction 发票扣款
     * @return 结果
     */
    @Override
    public int insertCldInvoiceDeduction(CldInvoiceDeduction cldInvoiceDeduction)
    {
        CldInvoice invoice = cldInvoiceMapper.selectCldInvoiceByInvoiceId(cldInvoiceDeduction.getInvoiceId());
        if(null != invoice){
            invoice.setAnswerInvoiceAmount(invoice.getAnswerInvoiceAmount().subtract(cldInvoiceDeduction.getInvoiceMoney()));
            cldInvoiceMapper.updateCldInvoice(invoice);
        }else{
            throw new RuntimeException("获取发票信息错误!");
        }
        cldInvoiceDeduction.setCreateTime(DateUtils.getNowDate());
        cldInvoiceDeduction.setCreateId(SecurityUtils.getUserId());
        cldInvoiceDeduction.setCreateName(SecurityUtils.getUsername());
        cldInvoiceDeduction.setFlag(0L);
        return cldInvoiceDeductionMapper.insertCldInvoiceDeduction(cldInvoiceDeduction);
    }

    /**
     * 修改发票扣款
     * 
     * @param cldInvoiceDeduction 发票扣款
     * @return 结果
     */
    @Override
    public int updateCldInvoiceDeduction(CldInvoiceDeduction cldInvoiceDeduction)
    {
        return cldInvoiceDeductionMapper.updateCldInvoiceDeduction(cldInvoiceDeduction);
    }

    /**
     * 批量删除发票扣款
     * 
     * @param invoiceDeductionIds 需要删除的发票扣款主键
     * @return 结果
     */
    @Override
    public int deleteCldInvoiceDeductionByInvoiceDeductionIds(Long[] invoiceDeductionIds)
    {
        if(invoiceDeductionIds.length != 0){
            for (int i=0;i<invoiceDeductionIds.length;i++){
                CldInvoiceDeduction cldInvoiceDeduction = cldInvoiceDeductionMapper.selectCldInvoiceDeductionByInvoiceDeductionId(invoiceDeductionIds[i]);
                if(null != cldInvoiceDeduction){
                    CldInvoice invoice = cldInvoiceMapper.selectCldInvoiceByInvoiceId(cldInvoiceDeduction.getInvoiceId());
                    if(null != invoice){
                        invoice.setAnswerInvoiceAmount(invoice.getAnswerInvoiceAmount().add(cldInvoiceDeduction.getInvoiceMoney()));
                        cldInvoiceMapper.updateCldInvoice(invoice);
                    }else{
                        throw new RuntimeException("获取发票信息错误!");
                    }
                }else{
                    throw new RuntimeException("获取发票扣款信息错误!");
                }
            }
        }
        return cldInvoiceDeductionMapper.deleteCldInvoiceDeductionByInvoiceDeductionIds(invoiceDeductionIds);
    }

    /**
     * 删除发票扣款信息
     * 
     * @param invoiceDeductionId 发票扣款主键
     * @return 结果
     */
    @Override
    public int deleteCldInvoiceDeductionByInvoiceDeductionId(Long invoiceDeductionId)
    {
        return cldInvoiceDeductionMapper.deleteCldInvoiceDeductionByInvoiceDeductionId(invoiceDeductionId);
    }
}
